﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BlueSkySOA.Core.Logging;
using Castle.Windsor;
using Castle.Facilities.WcfIntegration;
using Castle.MicroKernel.Registration;
using BlueSkySOA.Service.SMSService.ApplicationService;
using BlueSkySOA.Service.SMSService.ApplicationService.Impl;
using BlueSkySOA.Core.ProxyServer;
using BlueSkySOA.Service.SMSService;
using BlueSkySOA.Service.SMSService.Impl;
using BlueSkySOA.Core.Configuration;
using BlueSkySOA.Core.Logging;


namespace BlueSkySOA
{
    public class BlueSkySOAApplication : HttpApplication, IContainerAccessor
    {
        private static IWindsorContainer _windsorContainer;
        public IWindsorContainer Container
        {
            get { return _windsorContainer; }
        }
        public static IWindsorContainer WindsorContainer
        {
            get { return _windsorContainer; }
        }


        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            _windsorContainer = new WindsorContainer();
            Container.AddFacility<WcfFacility>().Register(
                Component.For<ISmsApplicationService>().ImplementedBy<SmsApplicationService>().Named("SmsApplicationService"),
                Component.For<IServerProxy>().ImplementedBy<DefaultServerProxy>().Named("ServerProxy"),
                Component.For<ISmsService>()
                      .ImplementedBy<SmsService>()
                      .Named("SmsService")                
            );
            ApplicationSettingsFactory.InitializeApplicationSettingsFactory(new WebConfigApplicationSettings());
            LoggingFactory.InitializeLogFactory(new Log4NetAdapter());
            LoggingFactory.GetLogger().Log("Bluesky SOA Application start");
        }


        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            // Code that runs when an unhandled error occurs
            if (_windsorContainer != null)
            {
                _windsorContainer.Dispose();
            }
        }
        void Application_Error(object sender, EventArgs e)
        {

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }
    }
}
